dnl  vim: set sw=4 expandtab :
dnl
dnl  Licensed under the Apache License, Version 2.0 (the "License");
dnl  you may not use this file except in compliance with the License.
dnl  You may obtain a copy of the License at
dnl
dnl      http://www.apache.org/licenses/LICENSE-2.0
dnl
dnl  Unless required by applicable law or agreed to in writing, software
dnl  distributed under the License is distributed on an "AS IS" BASIS,
dnl  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
dnl  See the License for the specific language governing permissions and
dnl  limitations under the License.

dnl Process this file with autoconf to produce a configure script.

m4_define([pyhp_major_version], [0])
m4_define([pyhp_minor_version], [0])
m4_define([pyhp_micro_version], [0])
m4_define([pyhp_interface_age], [0])
m4_define([pyhp_binary_age],
          [m4_eval(100 * pyhp_minor_version + pyhp_micro_version)])
m4_define([pyhp_version],
          [pyhp_major_version.pyhp_minor_version.pyhp_micro_version])
m4_define([pyhp_lt_current],
          [m4_eval(100 * pyhp_minor_version + pyhp_micro_version - pyhp_interface_age)])
m4_define([pyhp_lt_revision], [pyhp_interface_age])
m4_define([pyhp_lt_age], [m4_eval(pyhp_binary_age - pyhp_interface_age)])

AC_INIT(libpyhp, [pyhp_version])

AC_CONFIG_AUX_DIR(config)
AM_INIT_AUTOMAKE
AC_ARG_WITH(apxs, AC_HELP_STRING([--with-apxs=NAME],
            [name of the apxs executable [[apxs]]]),
            [APXS="$with_apxs"])


m4_undefine([AC_PROG_CXX])
m4_defun([AC_PROG_CXX],[])
m4_undefine([AC_PROG_F77])
m4_defun([AC_PROG_F77],[])
AC_PROG_LIBTOOL
LIBTOOL="$LIBTOOL --silent"
AM_PROG_CC_C_O

if test -z "${APXS}"; then
    AC_PATH_PROGS(APXS, apxs2 apxs, [apxs],
                   [$PATH:/usr/sbin])
fi

PKG_CHECK_MODULES(LIBAPR,[apr-1 >= 1.2])
CFLAGS="$CFLAGS $LIBAPR_CFLAGS"
LIBS="$LIBS $LIBAPR_LIBS"
AC_SUBST(LIBAPR_LIBS)

CFLAGS="$CFLAGS $($APXS -q CFLAGS_SHLIB)"
LIBS="$CFLAGS $($APXS -q LIBS_SHLIB)"
CFLAGS="$CFLAGS -I$($APXS -q INCLUDEDIR)"

AC_ARG_WITH(python, AC_HELP_STRING([--with-python=NAME],
            [name of the python executable [[python]]]),
            [PYTHON="$with_python"])

if test -z "${PYTHON}"; then
    AC_PATH_PROGS(PYTHON, python, [python],
                   [$PATH])
fi

AC_SUBST(PYTHON)

PYTHON_VERSION=`${PYTHON} -c 'from distutils import sysconfig; \
    print sysconfig.get_config_var("VERSION")'`

CPPFLAGS1=`${PYTHON} -c 'from distutils import sysconfig; \
    print "-I" + sysconfig.get_config_var("INCLUDEPY")'`

CPPFLAGS2=`${PYTHON} -c 'from distutils import sysconfig; \
    print " ".join(filter(lambda x: x.startswith("-D"), \
    sysconfig.get_config_var("CFLAGS").split()))'`

CFLAGS="$CFLAGS ${CPPFLAGS1} ${CPPFLAGS2}"

PYTHONFRAMEWORKDIR=`${PYTHON} -c 'from distutils import sysconfig; \
    print sysconfig.get_config_var("PYTHONFRAMEWORKDIR")'`

PYTHONFRAMEWORK=`${PYTHON} -c 'from distutils import sysconfig; \
    print sysconfig.get_config_var("PYTHONFRAMEWORK")'`

if test "${PYTHONFRAMEWORKDIR}" = "no-framework"; then
    LDFLAGS=`${PYTHON} -c 'import distutils.sysconfig; \
        print "-L" + distutils.sysconfig.get_python_lib(plat_specific=1, \
        standard_lib=1) +"/config"'`

    LDLIBS1="-lpython${PYTHON_VERSION}"
    LDLIBS2=`${PYTHON} -c 'from distutils import sysconfig; \
        print sysconfig.get_config_var("LIBS")'`

    LDLIBS="${LDLIBS1} ${LDLIBS2}"
else
    LDFLAGS1="-framework ${PYTHONFRAMEWORK}"

    VERSION="${PYTHON_VERSION}"
    STRING="${PYTHONFRAMEWORKDIR}/Versions/${VERSION}/${PYTHONFRAMEWORK}"
    LDFLAGS2=`${PYTHON} -c "from distutils import sysconfig; \
        print sysconfig.get_config_var(\"LINKFORSHARED\").replace( \
        \"${STRING}\", '')"`

    LDFLAGS="${LDFLAGS1} ${LDFLAGS2}"

    LDLIBS=`${PYTHON} -c 'from distutils import sysconfig; \
        print sysconfig.get_config_var("LIBS")'`
fi

LIBS="$LIBS $LDFLAGS $LDLIBS"

AC_MSG_CHECKING(Apache version)
HTTPD="`${APXS} -q SBINDIR`/`${APXS} -q TARGET`"
HTTPD_VERSION=`$HTTPD -v | awk '/version/ {print $3}' | awk -F/ '{print $2}'`
AC_MSG_RESULT($HTTPD_VERSION)

moddir="`${APXS} -q LIBEXECDIR`"
AC_SUBST(moddir)

HTTPD_MAJOR_VERSION=`echo ${HTTPD_VERSION} | sed -e 's/\..*//'`

if test $HTTPD_MAJOR_VERSION != 2 ; then
    AC_MSG_ERROR([Apache $HTTPD_VERSION not supported])
fi

AX_LIB_MYSQL([5.0])

AC_ARG_ENABLE([newparser],
              AS_HELP_STRING([--enable-newparser], [enable new ragel parser [[default=yes]]]),,
              [enable_newparser=yes])

AM_CONDITIONAL(ENABLE_NEWPARSER, [test "$enable_newparser" = "yes"])


LIBS="$LIBS $MYSQL_LDFLAGS"
CFLAGS="$CFLAGS $MYSQL_CFLAGS"

AC_CONFIG_FILES([Makefile
                 src/Makefile])

#                 examples/Makefile])

AC_OUTPUT
